﻿
EE_RepairFrame = CreateFrame("Frame", "EE_RepairFrame");

EERepair_AutoRepairTooltip = CreateFrame("GameTooltip", "EERepair_AutoRepairTooltip");

EE_RepairFrame:SetScript("OnEvent",
	function() 
		if (event == "MERCHANT_SHOW") then
			EE_RepairFrame:EE_RepairMerchant()
		end
	end);

EE_RepairFrame:RegisterEvent("MERCHANT_SHOW");

EE_RepairtotalCoin = 0;

EE_RepairValueTip = CreateFrame("GameTooltip", "EE_RepairValueTip");

EE_RepairValueTip:SetScript("OnTooltipAddMoney",
	function()
		if (arg1) then
			EE_RepairtotalCoin = EE_RepairtotalCoin + arg1;
		end
	end
	);

function EE_RepairFrame:EE_Repair_GetCost(allcost)
	local str="";
	local gold, silver, copper;
	local SILVER			= "|c00C0C0C0";
	local COPPER			= "|c00CC9900";
	local GOLD			= "|c00FFFF66";
	copper = mod(floor(allcost + .5),				100);
	silver = mod(floor(allcost/100),				100);
	gold	 = mod(floor(allcost/(100*100)),	100);
	if ( gold > 0 ) then str = GOLD .. gold .. "金币" end;
				if ( silver > 0) then str = str .. SILVER .. silver .. "银币" end;
			if ( copper > 0 ) then str = str .. COPPER .. copper .. "铜币" end;
	return str;
end

function EE_RepairFrame:EE_Repair_SellItemInContainer(ContainerID)
	local i, total, mailtoname, mailtoreason;
	local slots = GetContainerNumSlots(ContainerID);
	for i=1, slots , 1 do
		local texture, itemCount, locked, quality, readable = GetContainerItemInfo(ContainerID,i);
		if (texture) then
			local ItemLink=GetContainerItemLink(ContainerID,i);
			for color, id, name in string.gmatch(ItemLink, "|c(%x+)|Hitem:(%d+:%d+:%d+:%d+:%d+:%d+:%d+:.%d+)|h%[(.-)%]|h|r") do

				if( color ~= nil and id ~= nil and name ~= nil ) then
					if (color == 'ff9d9d9d') then
						if not (name=="毛茸茸的小爪子" or name=="破损的熊皮" or name=="柔软的狼尾" or name=="鲜艳的羽毛" or name=="邪恶的蝙蝠眼" or name=="发光的蝎血") then
							if (MerchantFrame:IsVisible()) then
								--统计
								EE_RepairValueTip:SetBagItem(ContainerID, i);
								--统计								
								_,_,_,_,_,mailtoreason = GetAddOnInfo("MailTo")
								--if(not mailtoreason) then
								--	MailTo_InvUse_Save(ContainerID, i);
								--else
									UseContainerItem(ContainerID, i);
								--end								
								--UseContainerItem(ContainerID, i);
							end
						end
					end
				end
			end
		end
	end
	total = EE_RepairtotalCoin;
	EE_RepairtotalCoin = 0;
	
	if (not total) then
		total = 0;
	end
	return total;
end

function EE_RepairFrame:EE_Repair_RepairInventory()
	if (CanMerchantRepair()) then
		ShowRepairCursor();
		local cost = 0;
		for bag = 0,NUM_BAG_FRAMES,1 do
			for slot = 1, GetContainerNumSlots(bag) , 1 do
				local hasCooldown, repairCost = EERepair_AutoRepairTooltip:SetBagItem(bag,slot);
				if (repairCost and repairCost > 0) then
					cost = cost + repairCost;
					PickupContainerItem(bag,slot);
				end
			end
		end
		HideRepairCursor();
		if (cost>0) then
			DEFAULT_CHAT_FRAME:AddMessage("修复背包内的装备花费: " .. EE_RepairFrame:EE_Repair_GetCost(cost), 1, 1, 0.5);
		end
	end
	return cost;
end

function EE_RepairFrame:EE_RepairMerchant()
	local ContainerID;

	local Sell, Repair = 0, 0;
	local Spell = "";

	local repairAllCost, canRepair = GetRepairAllCost();
	
	Sell = 0;
	
	for ContainerID = 0, NUM_BAG_FRAMES, 1 do
		Sell = Sell + EE_RepairFrame:EE_Repair_SellItemInContainer(ContainerID);
	end

	if (Sell > 0) then
		DEFAULT_CHAT_FRAME:AddMessage("售出物品得到:" .. EE_RepairFrame:EE_Repair_GetCost(Sell), 1, 1, 0.5);
	end

	if ( canRepair ) then
		RepairAllItems();
		DEFAULT_CHAT_FRAME:AddMessage("修理所有装备花费:" .. EE_RepairFrame:EE_Repair_GetCost(repairAllCost), 1, 1, 0.5);
	end
	Repair = EE_RepairFrame:EE_Repair_RepairInventory();

	if (not Repair) then
		Repair = 0;
	end

	if (Sell > 0 or repairAllCost > 0 or Repair > 0) then
		Sell = Sell - Repair - repairAllCost;
		if (Sell == 0) then
			DEFAULT_CHAT_FRAME:AddMessage("这次无收入", 1, 1, 0.5);
		else
			if (Sell < 0) then
				Sell = - Sell;
				Spell = RED_FONT_COLOR_CODE.."损失:"..FONT_COLOR_CODE_CLOSE;
			else
				Spell = GREEN_FONT_COLOR_CODE.."获利:"..FONT_COLOR_CODE_CLOSE;
			end
		end
		DEFAULT_CHAT_FRAME:AddMessage("这次交易共"..Spell..EE_RepairFrame:EE_Repair_GetCost(Sell), 1, 1, 0.5);
	end
end
